package com.lighting.module3;

import java.text.NumberFormat;
import java.util.Locale;

//等额本金 计算器
public class Calculator1 extends Calculator {
    //字段
//    double p;  //本金
//    int m;     //月份
//    double yr; //年利率

    //构造方法
    public Calculator1(double p, int m, double yr) {
        super(p, m, yr);
    }


    //普通方法
    @Override
    public String[] cal() {
        double mr = yr / 12.0 / 100.0;
        double payPrincipal = p / m;      //偿还本金
        double backup = p;                //备份本金
        double payInterestTotal = 0.0;    //总利息

        for (int i = 0; i < m; i++) {
            double payInterest = p * mr;  //偿还利息
            p -= payPrincipal;            //剩余本金
            payInterestTotal += payInterest;
        }
        //[0]还款总额 [1]总利息
        return new String[]{
                NumberFormat.getCurrencyInstance(Locale.CHINA).format(backup + payInterestTotal),
                NumberFormat.getCurrencyInstance(Locale.CHINA).format(payInterestTotal)
        };
    }

    @Override
    public String[][] details() {
        double payPrincipal = p / m;                     //偿还本金
        double mr = yr / 12.0 / 100.0;
        String[][] a2 = new String[m][];
        for (int i = 0; i < m; i++) {
            double payInterest = p * mr;                 //偿还利息
            p -= payPrincipal;                           //剩余本金
            double payment = payPrincipal + payInterest; //月供

            a2[i] = CreateRow(i, payment, payPrincipal, payInterest);
        }
        return a2;
    }

}
